/*
 * @Author: c
 * @Date: 2024-02-01 12:16:53
 * @LastEditTime: 2024-02-03 05:48:12
 * @LastEditors: c
 * @Description:
 * @FilePath: \music-web-admin\src\store\modules\audio.ts
 * https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { durationFormat } from "@/utils";
import { defineStore } from "pinia";

export const useAudioStore = defineStore("audio", () => {
  const currentTime = ref<number>(0);

  const lrcData = reactive<{
    audioUrl: string;
    album: string;
    title: string;
    editor: string;
    artist: string;
    lrcText: string;
    seekTime: number;
    closeAudio: boolean;
  }>({
    audioUrl: "",
    album: "",
    title: "",
    editor: "",
    artist: "",
    seekTime: 0,
    lrcText: "",
    closeAudio: false,
  });
  // const pureLrcData = toRaw(lrcData)

  const currentTimeStr = computed(
    () => `[${durationFormat(currentTime.value * 1000, true)}]`
  );

  const lrcText = computed(() => {
    let lrcHeader = [];
    if (lrcData.album) {
      lrcHeader.push(`[al:${lrcData.album}]`);
    }
    if (lrcData.artist) {
      lrcHeader.push(`[ar:${lrcData.artist}]`);
    }
    if (lrcData.editor) {
      lrcHeader.push(`[by:${lrcData.editor}]`);
    }
    if (lrcData.title) {
      lrcHeader.push(`[ti:${lrcData.title}]`);
    }
    let newLrc =
      lrcHeader.join("\n") +
      (lrcHeader.length > 0 ? "\n" : "") +
      lrcData.lrcText;

    return newLrc;
  });

  // watch(() => lrcData.closeAudio, (nv) => {

  //   if (nv) {
  //     Object.assign(lrcData, {
  //       audioUrl: "",
  //       album: "",
  //       title: "",
  //       editor: "",
  //       artist: "",
  //       seekTime: 0,
  //       lrcText: "",
  //       closeAudio: false,
  //     });
  //   }
  // })

  return { currentTime, currentTimeStr, lrcData, lrcText };
});
